// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, must_be_immutable
import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:junyeyishu/ui/home/QGNFTDetailPage.dart';

import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGButton.dart';

class QGSaleCopyrightNFTDetail extends QGNFTDetailPage {
  String id;
  QGSaleCopyrightNFTDetail({super.key, required this.id}) : super(id: id);
  @override
  State<QGNFTDetailPage> createState() => _QGSaleCopyrightNFTDetailState();
}

class _QGSaleCopyrightNFTDetailState extends QGNFTDetailPageState {
  StreamSubscription? subs;
  bool unlock = true;

  @override
  void dispose() {
    subs?.cancel();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();

    subs = eventBus.on<EventTradeOrderUnlock>().listen((obj) {
      unlock = false;
    });
  }

  @override
  handleData(data) {
    data?["price"] = data?["price_consignment"];
  }

  @override
  url() {
    return url_market_collectionInfo;
  }

  @override
  param() {
    return {"cid": widget.id};
  }

  @override
  buildOwner() {
    return buildIntroChainInfoItem(title: "拥有者", value: data?["owner"] ?? "");
  }

  @override
  buildIntroNo() {
    return Container(
      margin: EdgeInsets.only(top: 10.rpx),
      padding: EdgeInsets.fromLTRB(10.rpx, 15.rpx, 10.rpx, 15.rpx),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10.rpx),
        color: Color(0xFFF8F8F6),
      ),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          '产品编号',
          style: TextStyle(
              color: Color(0xFF111111),
              fontSize: 14.rpx,
              fontWeight: FontWeight.normal),
        ),
        Text(
          '#${data?["nft_no"]}/${data?["total"]}',
          style: TextStyle(
              color: Color(0xFF111111),
              fontSize: 14.rpx,
              fontWeight: FontWeight.bold),
        ),
      ]),
    );
  }

  @override
  buildBottom() {
    return Container(
      width: kScreenWidth,
      color: Color(0xFFFFFFFF),
      padding:
          EdgeInsets.fromLTRB(10.rpx, 10.rpx, 10.rpx, max(kSafeBottom, 10)),
      child: Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
        Text(
          '￥${data?["price_consignment"]}',
          style: TextStyle(
              color: Color(0xFF111111),
              fontSize: 26.rpx,
              fontWeight: FontWeight.bold),
        ),
        Flexible(
          child: Container(
              alignment: Alignment.center,
              height: 44.rpx,
              width: 210.rpx,
              child: QGButton(
                title: "去支付",
                enable: true,
                onTap: onPay,
              )),
        )
      ]),
    );
  }

  @override
  pid() {
    return data?["pid"];
  }

  @override
  onPay() async {
    if (!User.isLogin) {
      Navigator.of(context).pushNamed(PageLogin);
      return;
    }

    //是否已开通钱包
    if (!await checkHF()) {
      return;
    }
    data?["order_price"] = data?["price_consignment"];
    Navigator.of(context)
        .pushNamed(PageTradeOrderConfirm, arguments: data)
        .then((value) {});
  }
}
